{include file="common/header"}
<body class="pear-container">
<div class="layui-row layui-col-space10">
    <div class="layui-col-md12">
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form top-search-from" action="">
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">训练名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="task_title" placeholder="" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="query">
                            <i class="layui-icon layui-icon-search"></i>查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md">
                            <i class="layui-icon layui-icon-refresh"></i>重置
                        </button>
                    </div>
                    <div class="toggle-btn">
                        <a class="layui-hide">展开<i class="layui-icon layui-icon-down"></i></a>
                        <a class="layui-hide">收起<i class="layui-icon layui-icon-up"></i></a>
                    </div>
                </form>
            </div>
        </div>
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="dict-table" lay-filter="dict-table"></table>
            </div>
        </div>
    </div>
</div>

</body>
<script type="text/html" id="dict-brand-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script type="text/html" id="dict-province-bar">
    <button class="pear-btn pear-btn-primary pear-btn-xs" lay-event="edit">
        <i class="layui-icon">编辑</i>
    </button>
    <button class="pear-btn pear-btn-warming pear-btn-xs" lay-event="train">
        <i class="layui-icon">计划</i>
    </button>
</script>

<script type="text/html" id="status">
    <input type="checkbox" name="task_status" value="{{d.train_id}}" lay-skin="switch" lay-text="启用|禁用"
           lay-filter="changeStatus"
           {{# if(d.task_status==1){ }} checked {{# } }}>
</script>
{include file="common/footer"}
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'dropdown'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        let MODULE_PATH = "{$Request.root}/Train/";

        table.render({
            elem: '#dict-table',
            url: MODULE_PATH + 'task',
            cols: [
                [
                    {type: 'checkbox'},
                    {title: 'ID', field: 'train_id', align: 'center', width: 60},
                    {title: '制定方式', field: 'train_type', align: 'center'},
                    {title: '训练名称', field: 'train_title', align: 'center'},
                    {title: '开始时间', field: 'begin_time', align: 'center'},
                    {title: '结束时间', field: 'end_time', align: 'center'},
                    {title: '用户名称', field: 'user_name', align: 'center'},
                    {title: '用户电话', field: 'user_mobile', align: 'center'},
                    {
                        title: '训练计划', field: 'train_times', align: 'center', templet: function (d) {
                            return d.train_times + '项';
                        }
                    },
                    // {
                    //     title: '训练视频', field: 'train_video', align: 'center', templet: function (d) {
                    //         if (d.train_status) return '<a style="color: #1E9FFF;cursor: pointer;" target="_blank" href="' + d.train_video + '">点击查看</a>';
                    //         else return '暂未训练';
                    //     }
                    // },
                    {title: '创建时间', field: 'create_time', align: 'center'},
                    {title: '操作', toolbar: '#dict-province-bar', fixed: 'right', align: 'center', width: 135}
                ]
            ],
            cellMinWidth: 100,
            skin: 'grid',
            even: 'true',
            size: 'lg',
            page: true,
            toolbar: '#dict-brand-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter']
        });

        form.on('submit(query)', function (data) {
            table.reload('dict-table', {
                where: data.field,
                page: {curr: 1}
            })
            return false;
        });

        table.on('tool(dict-table)', function (obj) {
            if (obj.event === 'edit') {
                window.edit(obj.data['train_id']);
            } else if (obj.event === 'train') {
                window.train(obj);
            }
        });
        table.on('toolbar(dict-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: '添加训练任务',
                offset: 'r',
                anim: 'slideLeft', // 从右往左
                shade: 0.1,
                shadeClose: true,
                area: common.screenSide(),
                content: MODULE_PATH + 'addTask'
            });
        }

        window.edit = function (id) {
            layer.open({
                type: 2,
                title: '编辑训练任务',
                offset: 'r',
                anim: 'slideLeft', // 从右往左
                shade: 0.1,
                shadeClose: true,
                area: common.screenSide(),
                content: MODULE_PATH + 'editTask/id/' + id
            });
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }
            var ids = []
            var hasCheck = table.checkStatus('dataTable')
            var hasCheckData = hasCheck.data
            if (hasCheckData.length > 0) {
                $.each(hasCheckData, function (index, element) {
                    ids.push(element.id)
                })
            }
            layer.confirm('确定要删除这些数据', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + 'removeTask',
                    data: {ids: ids},
                    dataType: 'json',
                    type: 'POST',
                    success: function (res) {
                        layer.close(loading);
                        //判断有没有权限
                        if (res && res.code == 999) {
                            layer.msg(res.msg, {
                                icon: 5,
                                time: 2000,
                            })
                            return false;
                        } else if (res.code == 200) {
                            layer.msg(res.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('dataTable');
                            });
                        } else {
                            layer.msg(res.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.train = function (obj) {
            layer.open({
                type: 2,
                maxmin: true,
                title: '训练计划',
                shade: 0.1,
                area: common.screenLongMid(),
                content: MODULE_PATH + 'trainList/id/' + obj.data['train_id']
            });
        }

        window.refresh = function () {
            table.reload('dict-table');
        }

        form.on('switch(changeStatus)', function (data) {
            var status = data.elem.checked ? 1 : 0;
            var id = this.value;
            var load = layer.load();
            $.post('statusTask', {status: status, id: id}, function (res) {
                layer.close(load);
                //判断有没有权限
                if (res && res.code == 999) {
                    layer.msg(res.msg, {
                        icon: 5,
                        time: 1500,
                    }, function () {
                        window.refresh();
                    })
                    return false;
                } else if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1, time: 1500})
                } else {
                    layer.msg(res.msg, {icon: 2, time: 1500}, function () {
                        $(data.elem).prop('checked', !$(data.elem).prop('checked'));
                        form.render()
                    })
                }
            })
        });
    })
</script>
</html>
